package org.flint92.scalibaba.nacos.client.controller;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @author flint92
 */
@RestController
public class HelloController {

  @Resource
  private LoadBalancerClient loadBalancerClient;

  private final RestTemplate restTemplate;

  public HelloController() {
    this.restTemplate = new RestTemplate();
  }


  @GetMapping("/hello")
  public String hello(@RequestParam String name) {
    ServiceInstance serviceInstance = loadBalancerClient.choose("nacos-server");
    String uri = serviceInstance.getUri() + "/hello?name=" + name;

    String result = restTemplate.getForObject(uri, String.class);
    return "Invoke = " + uri + ", return: " + result;
  }

}
